package club.emergency.wechat.ranking.search.consumer.controller;

import club.emergency.wechat.common.response.ResponseEntity;
import club.emergency.wechat.common.response.StatusCode;
import club.emergency.wechat.common.utils.JsonConvert;
import club.emergency.wechat.ranking.search.consumer.service.RankingSearchConsumer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * Create with IntelliJ IDEA
 * Project Name: wechat
 * Package Name: club.emergency.wechat.ranking.search.consumer.controller
 * Date: 2019/2/25
 *
 * @author: 快乐的小菌菌
 * Description:
 */
@Api("排行榜查询api")
@RestController
@RequestMapping("/ranking-search")
public class RankingSearchController {

    private RankingSearchConsumer rankingSearchConsumer;

    public RankingSearchController(RankingSearchConsumer rankingSearchConsumer) {
        this.rankingSearchConsumer = rankingSearchConsumer;
    }

    @PostMapping("/rankingList")
    @ApiOperation("排行榜查询,默认值显示10个人的数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "endIndex", value = "结束index,从0开始，代表获取第一个人的数据！", defaultValue = "9", dataType = "int", paramType = "query")
    })
    public ResponseEntity rankingList(
            @RequestParam("endIndex") Integer endIndex
    ) {
        String rankingList = rankingSearchConsumer.rankingList(endIndex);
        if (Objects.isNull(rankingList)) {
            return new ResponseEntity(StatusCode.DB_SEARCH_OK, "排行榜为空！");
        }
        return new JsonConvert<ResponseEntity>().jsonDecode(rankingList, ResponseEntity.class);
    }
}
